package com.hbm.usercenter.activity;

import android.content.Intent;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.hbm.usercenter.R;
import com.hbm.usercenter.base.BaseActivity;
import com.hbm.usercenter.base.LoadingPager.LoadedResult;
import com.hbm.usercenter.bean.UserInfoBean;
import com.hbm.usercenter.protocol.MyInfoProtocol;
import com.hbm.usercenter.utils.LogUtils;
import com.hbm.usercenter.utils.UIUtils;
import com.hbm.usercenter.widget.GroupInfo;

import butterknife.Bind;
import butterknife.ButterKnife;

import static com.hbm.usercenter.R.id.toolbar_title;

public class MyUserInfoActivity extends BaseActivity {

    @Bind(R.id.user_name)
    GroupInfo mUserName;
    @Bind(R.id.up_name)
    GroupInfo mUpName;
    @Bind(R.id.user_id)
    GroupInfo mUserId;
    @Bind(R.id.mobile)
    GroupInfo mMobile;
    @Bind(R.id.user_group)
    GroupInfo mUserGroup;
    @Bind(R.id.bank_card)
    GroupInfo mBankCard;
    @Bind(R.id.account_name)
    GroupInfo mAccountName;
    @Bind(toolbar_title)
    TextView mToolbarTitle;
    @Bind(R.id.toolbar)
    Toolbar mToolbar;
    private MyInfoProtocol mProtocol;
    private UserInfoBean mData;

    @Override
    protected LoadedResult initData() {
        mProtocol = new MyInfoProtocol();
        try {
            mData = mProtocol.loadData(0);
            LogUtils.s(mData.toString());
            LoadedResult checkState = checkState(mData);
            return checkState;
        } catch (Exception e) {
            e.printStackTrace();
            return LoadedResult.ERROR;
        }
    }

    @Override
    protected View initSuccessView() {
        View view = View.inflate(this, R.layout.activity_my_user_info, null);
        //        View view = LayoutInflater.from(this).inflate(R.layout.activity_my_user_info,
        // null);

        ButterKnife.bind(this, view);
        initToolbar("我的资料");
        initView();
        return view;
    }


    protected void initView() {
        mUpName.setText(mData.getUp_name());
        mUserGroup.setText(mData.getUser_group());

        UserInfoBean.UserinfoBean userinfo = mData.getUserinfo();
        mMobile.setText(userinfo.getMobile());
        mUserId.setText(userinfo.getUser_id());
        mUserName.setText(userinfo.getUser_name());

        UserInfoBean.WithdrawinfoBean withdrawinfo = mData.getWithdrawinfo();
        mAccountName.setText(withdrawinfo.getAccount_name());
        mBankCard.setText(withdrawinfo.getBank_card());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //    @Override
    //    public boolean onOptionsItemSelected(MenuItem item) {
    //        switch (item.getItemId()) {
    //            case R.id.edit_item:
    //                startActivity(new Intent(this,UpdateActivity.class));
    //                break;
    //            case R.id.home:
    //                finish();
    //                break;
    //
    //        }
    //        return true;
    //    }
    public void initToolbar(String title) {
        setSupportActionBar(mToolbar);
        mToolbarTitle.setText(title);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.edit_item:
                        startActivity(new Intent(UIUtils.getContext(), UpdateActivity.class));
                        break;
                }
                return true;
            }
        });
    }

}
